<?php

namespace App\Console\Commands;

use App\Models\Material;
use Illuminate\Console\Command;
use Monolog\Handler\StreamHandler;
use Monolog\Logger;

class MaterialVideo extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'material:video';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '检查素材视频是否转码成功';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        $file_name = storage_path().'/logs/command/material_video_'.date('Ym').'.log';
        $log = new Logger('command');
        $log->pushHandler(new StreamHandler($file_name, Logger::INFO));
        $log->info('Start');
        
        $list = Material::where([
                ['type',Material::TYPE_VIDEO],
                ['is_system',Material::IS_SYSTEM_NO],
                ['path','=',''],
                ['convert_status',[
                    Material::CONVERT_STATUS_NOCONVERT,
                    Material::CONVERT_STATUS_CONVERTING
                ]],
            ])
            ->get();
        foreach ($list as $k=>$v)
        {
            $ccLib = app('cc');
            $ccLib->config([
                'user_id' => config('cc.user_id'),
                'key' => config('cc.key'),
                'base_url' => config('cc.mobile_url')
            ]);
            $params = [
                'videoid' => $v->key, 'ip' => request()->getClientIp()
            ];
            $video_info = $ccLib->videoMobileUrl($params);
            if ($video_info['status'])
            {
                $item = Material::findOrFail($v->id);
                $item->path = $video_info['data'];
                $item->convert_status = Material::CONVERT_STATUS_CONVERTED;
                $item->save();
                $log->notice('ID:.'.$v->id,[
                    'key' => $params,
                    'info' => $video_info
                ]);
            }
            else
            {
                $log->error('ID:.'.$v->id,[
                    'key' => $params,
                    'info' => $video_info
                ]);
            }
        }
        $log->info('End');
    }
}
